package com.zsy.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * @author: zhengsuyang
 * @Desc:
 * @create: 2025-09-14 10:02
 **/
@Slf4j
/*@Component
@Aspect*/
public class RecordTimeAspect {
    @Around("execution(* com.zsy.service.impl.*.*(..))")
    public Object recordTime(ProceedingJoinPoint pjp) throws Throwable{
        // 1.记录方法执行开始时间
        long begin = System.currentTimeMillis();
        // 执行原来方法
        Object proceed = null;
        try {
            proceed = pjp.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        // 获取类名
        String className = pjp.getTarget().getClass().getName();
        // 获取方法名
        MethodSignature signature = (MethodSignature)pjp.getSignature();
        String methodName = signature.getMethod().getName();
        // 2.记录方法执行结束时间
        long end = System.currentTimeMillis();
        log.info( "{}的{}方法执行的耗时:{}ms", className,methodName,end - begin);
        return proceed;
    }
}
